package com.bridgeintelligent.tag.bulkload.service.core.threadpool;

import com.bridgeintelligent.tag.bulkload.api.BulkLoadConfig;
import com.bridgeintelligent.tag.bulkload.service.es.ESUtils;
import lombok.AllArgsConstructor;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * Created by JackCheung on 2019/9/19 8:20 PM.
 */
@AllArgsConstructor
public class RealTimeThread implements Runnable {

    //CountDownLatch
    private CountDownLatch countDownLatch;
    //索引名
    private String indexName;
    //新增修改map
    private Map<String, Map<String, Object>> map;
    //删除map
    private Map<String, String> delMap;
    //类型
    private int type;
    //索引类型
    private String indexType;

    /**
     * 线程任务，批量处理数据
     */
    @Override
    public void run() {
        try {
            switch (type) {
                case BulkLoadConfig.FIRST_ADD_TYPE:
                case BulkLoadConfig.ADD_TYPE:
                case BulkLoadConfig.BATCH_TYPE:
                    //批量更新
                    ESUtils.bulkUpdate(indexName, indexType, map);
                    break;
                case BulkLoadConfig.DEL_TYPE:
                    //批量删除
                    ESUtils.bulkUpdateWithDel(indexName, indexType, delMap);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //计数器减1
            countDownLatch.countDown();
        }
    }
}
